package com.piggy.logi.sys.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.piggy.common.core.enums.CacheKeyEnums;
import com.piggy.common.core.exception.GlobalException;
import com.piggy.common.redis.cache.CacheUtils;
import com.piggy.common.satoken.utils.SecurityUtils;
import com.piggy.logi.sys.bo.LogiSysBaseConfigEditBo;
import com.piggy.logi.sys.domain.LogiSysBaseConfig;
import com.piggy.logi.sys.enums.BizNoPrefixEnum;
import com.piggy.logi.sys.mapper.LogiSysBaseConfigMapper;
import com.piggy.logi.sys.service.ILogiSysBaseConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * 基础业务设置Service业务层处理
 *
 * @author fengxy
 * @date 2023/10/17 21:54
 **/
@Slf4j
@Service
public class LogiSysBaseConfigServiceImpl extends ServiceImpl<LogiSysBaseConfigMapper, LogiSysBaseConfig> implements ILogiSysBaseConfigService {

    @Override
    public LogiSysBaseConfig getBaseConfig(Long deptId) {
        LogiSysBaseConfig sysBaseConfig = getOne(Wrappers.lambdaQuery(LogiSysBaseConfig.class)
                .eq(LogiSysBaseConfig::getDeptId, deptId));
        if (ObjectUtil.isNull(sysBaseConfig)) {
            sysBaseConfig = new LogiSysBaseConfig();
            sysBaseConfig.setDeptId(deptId);
            sysBaseConfig.setDepartPrefixStatus(0);
            sysBaseConfig.setDepartPrefix("");
            sysBaseConfig.setDrayagePrefixStatus(0);
            sysBaseConfig.setDrayagePrefix("");
            sysBaseConfig.setOrderPrefix("");
            sysBaseConfig.setSettledCheckBill(0);
            sysBaseConfig.setCheckBillInvoice(0);
            sysBaseConfig.setAgentPaySettled(0);
            sysBaseConfig.setReceivableSettled(0);
            sysBaseConfig.setPayableSettled(0);
        }
        return sysBaseConfig;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateInfo(LogiSysBaseConfigEditBo bo) {
        // 判断当前用户是不是超级管理员
        if (!SecurityUtils.isSysAdmin()) {
            throw new GlobalException("仅超级管理员可进行配置");
        }
        LogiSysBaseConfig sysBaseConfig = getBaseConfig(bo.getDeptId());
        sysBaseConfig.setDeptId(bo.getDeptId());
        sysBaseConfig.setDepartPrefixStatus(bo.getDepartPrefixStatus());
        sysBaseConfig.setDepartPrefix(bo.getDepartPrefix());
        sysBaseConfig.setDrayagePrefixStatus(bo.getDrayagePrefixStatus());
        sysBaseConfig.setDrayagePrefix(bo.getDrayagePrefix());
        sysBaseConfig.setOrderPrefix(StrUtil.blankToDefault(bo.getOrderPrefix(), ""));
        sysBaseConfig.setSettledCheckBill(bo.getSettledCheckBill());
        sysBaseConfig.setCheckBillInvoice(bo.getCheckBillInvoice());
        sysBaseConfig.setAgentPaySettled(bo.getAgentPaySettled());
        sysBaseConfig.setReceivableSettled(bo.getReceivableSettled());
        sysBaseConfig.setPayableSettled(bo.getPayableSettled());
        saveOrUpdate(sysBaseConfig);
    }

    @Override
    public String getScheduleNo(Long deptId) {
        LogiSysBaseConfig sysBaseConfig = getBaseConfig(deptId);
        if (sysBaseConfig.getDepartPrefixStatus() == 1) {
            return sysBaseConfig.getDepartPrefix() + DateUtil.format(DateUtil.date(), "yyMMdd") + RandomUtil.randomNumbers(3) + RandomUtil.randomString(2);
        }
        String date = DateUtil.format(new Date(), "yyyyMMdd");
        String key = CacheKeyEnums.LogiScheduleNo.getKey(date);
        if (!CacheUtils.hasKey(key)) {
            CacheUtils.cache(0, key, 2L, TimeUnit.DAYS);
        }
        Long no = CacheUtils.incrBy(key);
        return String.format("%s%s%06d", BizNoPrefixEnum.SCHEDULE_NO.getCode(), date, no);
    }

    @Override
    public String getDrayageNo(Long deptId) {
        LogiSysBaseConfig sysBaseConfig = getBaseConfig(deptId);
        if (sysBaseConfig.getDrayagePrefixStatus() == 1) {
            return sysBaseConfig.getDrayagePrefix() + DateUtil.format(DateUtil.date(), "yyMMdd") + RandomUtil.randomNumbers(3) + RandomUtil.randomString(2);
        }
        String date = DateUtil.format(new Date(), "yyyyMMdd");
        String key = CacheKeyEnums.LogiDrayageNo.getKey(date);
        if (!CacheUtils.hasKey(key)) {
            CacheUtils.cache(0, key, 2L, TimeUnit.DAYS);
        }
        Long no = CacheUtils.incrBy(key);
        return String.format("%s%s%06d", BizNoPrefixEnum.DRAYAGE_NO.getCode(), date, no);
    }

    @Override
    public String getGoodOrderNo(Long deptId) {
        LogiSysBaseConfig sysBaseConfig = getBaseConfig(deptId);
        return sysBaseConfig.getOrderPrefix() + DateUtil.format(DateUtil.date(), "yyMMdd");
    }

}
